// pages/my-pages/total/myTotal.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    allMemberNum: 0,
    addMemberNum: 0,
    continueMemberNum: 0,
    turnover: 0,
    topUpAmount: 0,
    nonmemberExpendAmount: 0,
    memberExpendAmount: 0,
    memberGiftExpendAmount: 0,
    date: new Date().getTime(), // 支持时间戳传入
    dateText: '',
    tabText: '日',
    allOrderNum: 0,
    memberOrderNum: 0,
    unMemberOrderNum: 0,
    useVoucherNum: 0,
    sendVoucherNum: 0,
    // 指定选择区间起始值
    start: '2020-01-01 00:00:00',
    end: '2050-09-09 23:59:59',
    dateVisibleDay: false,
    dateVisibleMonth: false,
    tabValue: 0,
    url: "dataCount/countShopDataByDay?date="
  },

  onTabsClick: function (e) {
    let tabValue = e.detail.value;
    let that = this;
    console.log(tabValue)
    that.setData({
      tabValue: tabValue
    })
    if (0 == tabValue) {
      that.setData({
        dateText: that.formaData(new Date()),
        url: "dataCount/countShopDataByDay?date=",
        tabText: '日'
      })
    } else {
      that.setData({
        dateText: that.formaData(new Date()),
        url: "dataCount/countShopDataByMonth?date=",
        tabText: '月'
      })
    }
    that.getCountInfo();
  },

  //点击查询日期, 弹出日期选择
  showPicker: function (e) {
    let that = this;
    let tabType = that.data.tabValue;
    if (0 == tabType) {
      this.setData({
        dateVisibleDay: true
      })
    } else {
      this.setData({
        dateVisibleMonth: true
      })
    }
  },

  //日期选择确定按钮
  onConfirm: function (e) {
    const {
      value
    } = e.detail;
    this.setData({
      date: value,
      dateText: value,
    });
    this.hidePicker();
    this.getCountInfo();
  },
  //日期选择弹窗改变事件
  onColumnChange: function (e) {
    console.log('选择时间:', e.detail.value);
  },
  //日期选择弹窗取消按钮
  hidePicker: function () {
    this.setData({
      dateVisibleDay: false,
      dateVisibleMonth: false,
    })
  },
  //查询基础数据统计
  getCountInfo: function () {
    let that = this;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: that.data.url + that.data.dateText
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.setData({
          allMemberNum: res.data.data.allMemberNum,
          addMemberNum: res.data.data.addMemberNum,
          continueMemberNum: res.data.data.continueMemberNum,
          turnover: res.data.data.turnover,
          topUpAmount: res.data.data.topUpAmount,
          nonmemberExpendAmount: res.data.data.nonmemberExpendAmount,
          memberExpendAmount: res.data.data.memberExpendAmount,
          memberGiftExpendAmount: res.data.data.memberGiftExpendAmount,
          allOrderNum: res.data.data.allOrderNum,
          memberOrderNum: res.data.data.memberOrderNum,
          unMemberOrderNum: res.data.data.unMemberOrderNum,
          sendVoucherNum: res.data.data.sendVoucherNum,
          useVoucherNum: res.data.data.useVoucherNum
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //时间格式化
  formaData: function (timer) {
    let that = this;
    const year = timer.getFullYear()
    const month = timer.getMonth() + 1 // 由于月份从0开始，因此需加1
    const day = timer.getDate()
    if (that.data.tabValue == 0) {
      return `${that.pad(year, 4)}-${that.pad(month)}-${that.pad(day)}`
    } else {
      return `${that.pad(year, 4)}-${that.pad(month)}`
    }
  },

  // 定义具体处理标准
  // timeEl 传递过来具体的数值：年月日时分秒
  // total 字符串总长度 默认值为2
  // str 补充元素 默认值为"0"
  pad: function (timeEl, total = 2, str = '0') {
    return timeEl.toString().padStart(total, str)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("执行了 onLoad")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("执行了 onShow")
    let that = this;
    that.setData({
      dateText: that.formaData(new Date()),
    })
    that.getCountInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("执行了 onHide")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})